package com.test.vertx;

import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;

public class Application {
	
	public static void main(String[] args) {
		Vertx vertx = Vertx.vertx();
		Router router = Router.router(vertx);
		
		// deploy verticles
		// create a http server
		HttpServer server = vertx.createHttpServer();
		JsonObject result = new JsonObject();
		result.put("name", "luqiaolong");
		result.put("age", 20);
		result.put("sex", "male");
		
		router.get("/users/:id").handler(routingContext -> {
			HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions());
			HttpServerResponse response = routingContext.response();
			Future<JsonObject> future = Future.future();
			httpClient.getAbs("http://localhost:8080/users/21", rest -> {
				rest.bodyHandler(buffer -> {
					future.complete(buffer.toJsonObject());
				});
			});
			response.end(future.result().toString());
		});
		server.requestHandler(router :: accept).listen(8081);
	}

}
